package filter

import (
	"context"
	"gitee.com/kristas/booting-go/framework"
	"gitee.com/kristas/booting-go/framework/core/statement/types"
	"gitee.com/kristas/booting-go/framework/web"
	"github.com/gofrs/uuid"
	"net/http"
)

func init() {
	framework.Component(new(CtxFilter))
}

type CtxFilter struct {
	types.Filter `order:"-1" pattern:"/*"`
}

func (c *CtxFilter) DoFilter(w http.ResponseWriter, r *http.Request, chain web.FilterChain) error {
	requestId, _ := uuid.NewV4()
	ctx := context.WithValue(r.Context(), "request_id", requestId.String())
	w.Header().Set("Request-Id", requestId.String())
	*r = *r.WithContext(ctx)
	return chain.DoFilter(w, r)
}
